package com.zhou.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.zhou.dao.OrderSettingMapper;
import com.zhou.pojo.OrderSetting;
import com.zhou.service.OrderSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 预约设置服务
 */
@Service(interfaceClass = OrderSettingService.class)
@Transactional
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Override
    public void add(List<OrderSetting> list) {
        if (list != null && list.size() > 0) {
            for (OrderSetting orderSetting : list) {
                //检查此数据（日期）是否存在
                long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
                if (count > 0) {
                    //已经存在,执行更新操作
                    OrderSetting order = orderSettingMapper.selectByPrimaryKey(orderSetting.getId());
                    if (orderSetting.getNumber() > order.getNumber()) {
                        orderSettingMapper.editNumberByOrderDate(orderSetting);
                    } else {
                        throw new RuntimeException();
                    }
                } else {
                    //不存在,执行新建操作
                    orderSettingMapper.insert(orderSetting);
                }
            }
        }
    }

    @Override
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date + "-1";//2021-3-1
        String dateEnd = date + "-31";//2021-3-31
        HashMap<String, String> map = new HashMap<>();
        map.put("dateBegin", dateBegin);
        map.put("dateEnd", dateEnd);
        List<OrderSetting> list = orderSettingMapper.getOrderSettingByMonth(map);
        ArrayList<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            HashMap<String, Integer> orderSettingMap = new HashMap<>();
            orderSettingMap.put("date", orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number", orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations", orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }

    @Override
    public void editNumberByOrderDate(OrderSetting orderSetting) {
        long count = orderSettingMapper.findCountByOrderDate(orderSetting.getOrderDate());
        if (count > 0) {
            //当前日期已经进行了预约设置，需要进行修改操作
            OrderSetting order = orderSettingMapper.selectByPrimaryKey(orderSetting.getId());
            if (orderSetting.getNumber() > order.getNumber()) {
                orderSettingMapper.editNumberByOrderDate(orderSetting);
            } else {
                throw new RuntimeException();
            }
        } else {
            //当前日期没有进行预约设置，进行添加操作
            orderSettingMapper.insert(orderSetting);
        }
    }
}
